home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 Super Collection / Windows 95 Super Collection.iso / win95 / programm / secdel32 / ntrash11.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-05  |  10.8 KB  |  282 lines

  1.  
  2. // +--------------------------------------------------------------------------+
  3. // |                                                                          |
  4. // |          Secure File Deletion Example  11/4/93 by Mark Gamber            |
  5. // |                                                                          |
  6. // +--------------------------------------------------------------------------+
  7. // |                                                                          |
  8. // |   This utility accepts files from a drag-drop source such as File        |
  9. // |   Manager, zeroes out the contents and deletes the file, removing any    |
  10. // |   trace of it's data from the disk.                                      |
  11. // |                                                                          |
  12. // +--------------------------------------------------------------------------|
  13. // |                                                                          |
  14. // |   This program is PUBLIC DOMAIN and may be freely distributed. In using  |
  15. // |   the program or any part thereof, the user assumes responsibility for   |
  16. // |   it's proper use.                                                       |
  17. // |                                                                          |
  18. // +--------------------------------------------------------------------------+
  19.  
  20. #include "windows.h"
  21. #include "memory.h"
  22. #include "ntrash11.h"
  23.  
  24.  
  25. HANDLE hInst;                                                  //  Some globals
  26. HWND MainWin;
  27. BOOL StayOnTop = TRUE;
  28. char *IniSection = "NTRASH";
  29. char *IniFile = "NTRASH.INI";                      //  Various string constants
  30. char *CLASSNAME = "SECDEL";
  31. char *XPOS = "XPOS";
  32. char *YPOS = "YPOS";
  33. char *SOT = "SOT";
  34. char *PD = "%d";
  35.  
  36. // +--------------------------------------------------------------------------+
  37. // |                                                                          |
  38. // |                                                                          |
  39. // +--------------------------------------------------------------------------+
  40.  
  41. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrev, LPSTR lpCmd, int nCmdShow )
  42. {
  43.    WNDCLASS wc;
  44.    HWND hWnd;
  45.    MSG msg;
  46.    UINT Xpos, Ypos;
  47.  
  48.    if( hPrev )
  49.       return( FALSE );
  50.  
  51.    wc.style = CS_DBLCLKS;                              //  Accept double-clicks
  52.    wc.lpfnWndProc = MainWndProc;
  53.    wc.cbClsExtra = 0;
  54.    wc.cbWndExtra = 0;
  55.    wc.hInstance = hInstance;
  56.    wc.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( 102 ) );
  57.    wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  58.    wc.hbrBackground = GetStockObject( LTGRAY_BRUSH );
  59.    wc.lpszMenuName = NULL;
  60.    wc.lpszClassName = CLASSNAME;
  61.  
  62.    if( ! RegisterClass( &wc ) )              // If class doesn't register, exit
  63.       return( FALSE );
  64.  
  65.    hInst = hInstance;
  66.                                        //  Get position when it was closed last
  67.    Xpos = GetPrivateProfileInt( IniSection, XPOS, 0, IniFile );
  68.    Ypos = GetPrivateProfileInt( IniSection, YPOS, 0, IniFile );
  69.                                       //  Does program "stay on top" of others?
  70.    StayOnTop = GetPrivateProfileInt( IniSection, SOT, 1, IniFile );
  71.  
  72.                                   //  Create the window accepting dropped files
  73.    hWnd = CreateWindowEx( WS_EX_ACCEPTFILES | WS_EX_TOPMOST, CLASSNAME,
  74.                           (LPSTR)"Test", 
  75.                                   WS_VISIBLE | WS_CLIPSIBLINGS | WS_POPUP,
  76.                           Xpos, Ypos, 48, 48, NULL, NULL, hInstance, NULL );
  77.    if( ! hWnd )
  78.       return( FALSE );                        // If create failed, exit program
  79.  
  80.    MainWin = hWnd;
  81.    ShowWindow( hWnd, SW_SHOWNORMAL );                         // Display window
  82.    UpdateWindow( hWnd );
  83.  
  84.    if( StayOnTop )              //  Do Z-order business if set to "stay on top"
  85.       SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0, 
  86.                           SWP_NOMOVE | SWP_NOSIZE );
  87.    else
  88.       SetWindowPos( hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, 
  89.                           SWP_NOMOVE | SWP_NOSIZE );
  90.  
  91.    while( GetMessage( &msg, 0, 0, 0 ) )                      // Do message loop
  92.    {
  93.       TranslateMessage( &msg );
  94.       DispatchMessage( &msg );
  95.    }
  96.    return( msg.wParam );
  97. }
  98.  
  99.  
  100. // +--------------------------------------------------------------------------+
  101. // |                                                                          |
  102. // |   MainWndProc(): This is the window that accepts drag-drop files.        |
  103. // |                                                                          |
  104. // +--------------------------------------------------------------------------+
  105.  
  106. long APIENTRY MainWndProc( HWND hWnd, UINT msg, UINT wParam, LONG lParam )
  107. {
  108.    switch( msg )
  109.    {
  110.       case WM_LBUTTONDOWN:             // If left button pressed, move window
  111.       {
  112.          PostMessage( hWnd, WM_SYSCOMMAND, 0xF012, 0x00110017 );
  113.          break;
  114.       }
  115.  
  116.       case WM_RBUTTONDOWN:                        // If right button pressed...
  117.       {                     // Note - no more of this MakeProcInstance nonsense
  118.          DialogBox( hInst, MAKEINTRESOURCE( 10000 ), hWnd, MainDlgProc );
  119.          break;
  120.       }
  121.  
  122.       case WM_LBUTTONDBLCLK:                               // Quit on double-click
  123.       {
  124.          RECT Rect;
  125.          char str[ 8 ];
  126.  
  127.          GetWindowRect( hWnd, &Rect );             //  Get our current position
  128.          wsprintf( str, PD, Rect.left );              //  and save it for later
  129.          WritePrivateProfileString( IniSection, XPOS, str, IniFile );
  130.          wsprintf( str, PD, Rect.top );
  131.          WritePrivateProfileString( IniSection, YPOS, str, IniFile );
  132.          wsprintf( str, PD, StayOnTop );
  133.          WritePrivateProfileString( IniSection, SOT, str, IniFile );
  134.  
  135.          PostQuitMessage( 0 );
  136.          break;
  137.       }
  138.  
  139.       case WM_DROPFILES:             // When files are dropped on the window...
  140.       {
  141.          WORD Files, Count;
  142.             DWORD dwSize;
  143.          char Filename[ 256 ];
  144.          HDC hDC;
  145.          HDC mDC;
  146.          HBITMAP hBmp;
  147.             HANDLE hFile;
  148.             HANDLE hMap;
  149.             LPSTR lpFile;
  150.                                                  // Get number of files dropped
  151.          Files = DragQueryFile( (HDROP)wParam, -1, Filename, 128 );
  152.          if( ! Files )                                    //  If no files, exit
  153.          {
  154.             DragFinish( (HDROP)wParam );
  155.             return( TRUE );
  156.          }
  157.  
  158.          hDC = GetDC( hWnd );
  159.          mDC = CreateCompatibleDC( hDC );          //  Draw can with the lid up
  160.          hBmp = LoadBitmap( hInst, MAKEINTRESOURCE( 100 ) );
  161.          SelectObject( mDC, hBmp );
  162.          BitBlt( hDC, 0, 0, 48, 48, mDC, 0, 0, SRCCOPY );
  163.          DeleteDC( mDC );
  164.          DeleteObject( hBmp );
  165.          ReleaseDC( hWnd, hDC );
  166.  
  167.          for( Count = 0; Count < Files; Count++ )     // Get every file dropped
  168.          {
  169.             DragQueryFile( (HDROP)wParam, Count, Filename, 128 );
  170.                                               //  Open each file for read/write
  171.                 hFile = CreateFile( Filename, GENERIC_READ | GENERIC_WRITE,
  172.                                           0, NULL, OPEN_EXISTING, 
  173.                                           FILE_ATTRIBUTE_NORMAL, NULL );
  174.  
  175.                 if( hFile == INVALID_HANDLE_VALUE )  //  If unable to open, skip it
  176.                 {
  177.                     dwSize = GetLastError();       //  These calls are for debugging
  178.                     continue;
  179.                 }
  180.  
  181.                 dwSize = GetFileSize( hFile, NULL );        //  Get the file's size
  182.                 if( ! dwSize )
  183.                 {
  184.                     CloseHandle( hFile );
  185.                     DeleteFile( Filename );        //  If nothing to erase, just kill it
  186.                     continue;
  187.                 }
  188.                                                //  Create a file mapping object
  189.                 hMap = CreateFileMapping( hFile, NULL, PAGE_READWRITE, 
  190.                                       0, 0, NULL );
  191.                 if( ! hMap )
  192.                 {
  193.                     dwSize = GetLastError();        //  If it didn't work, next file
  194.                     CloseHandle( hFile );
  195.                     continue;
  196.                 }
  197.                                         //  Map the file into a memory space...
  198.                 lpFile = MapViewOfFile( hMap, FILE_MAP_WRITE, 0, 0, 0 );
  199.                 if( lpFile == NULL )
  200.                 {
  201.                     dwSize = GetLastError();              //  On error, back way out
  202.                     CloseHandle( hFile );
  203.                     CloseHandle( hMap );
  204.                     continue;
  205.                 }
  206.                 memset( lpFile, 0, dwSize );        //  Set the whole thing to zero
  207.                 FlushViewOfFile( lpFile, dwSize );             //  Flush it to disk
  208.                 UnmapViewOfFile( lpFile );                     //  Kill mapped area
  209.  
  210.                 CloseHandle( hMap );                // Close all the handles and...
  211.                 CloseHandle( hFile );
  212.             DeleteFile( Filename );            //  ...(finally) delete the file
  213.          }
  214.          DragFinish( (HDROP)wParam );                  // Tell SHELL we're done
  215.          InvalidateRect( hWnd, NULL, FALSE );          // Repaint with lid down
  216.          return( TRUE );
  217.       }
  218.  
  219.       case WM_PAINT:
  220.       {
  221.          PAINTSTRUCT ps;
  222.          HDC hDC;
  223.          HDC mDC;
  224.          HBITMAP hBmp;
  225.  
  226.          hDC = BeginPaint( hWnd, &ps );         // Blasts out can with lid down
  227.          mDC = CreateCompatibleDC( hDC );
  228.          hBmp = LoadBitmap( hInst, MAKEINTRESOURCE( 101 ) );
  229.          SelectObject( mDC, hBmp );
  230.          BitBlt( hDC, 0, 0, 48, 48, mDC, 0, 0, SRCCOPY );
  231.          DeleteDC( mDC );
  232.          DeleteObject( hBmp );
  233.          EndPaint( hWnd, &ps );
  234.          break;
  235.       }
  236.  
  237.       case WM_DESTROY:
  238.          PostQuitMessage( 0 );
  239.          break;
  240.  
  241.       default:
  242.          return( DefWindowProc( hWnd, msg, wParam, lParam ) );
  243.    }
  244.    return( FALSE );
  245. }
  246.  
  247.  
  248. // +--------------------------------------------------------------------------+
  249. // |                                                                          |
  250. // |  MainDlgProc(): Get topmost setting from user                            |
  251. // |                                                                          |
  252. // +--------------------------------------------------------------------------+
  253.  
  254. BOOL APIENTRY MainDlgProc( HWND hDlg, UINT msg, UINT wParam, LONG lParam )
  255. {
  256.    switch( msg )
  257.    {
  258.       case WM_INITDIALOG:                                       // Set checkbox
  259.          CheckDlgButton( hDlg, IDM_STAYONTOP, StayOnTop );
  260.          return( TRUE );
  261.  
  262.       case WM_COMMAND:
  263.          if( wParam == IDCANCEL )                    // When Exit it pressed...
  264.          {                                        // Save 'Stay On Top' setting
  265.             StayOnTop = IsDlgButtonChecked( hDlg, IDM_STAYONTOP );
  266.  
  267.                                          // If checked, mark window accordingly
  268.             if( StayOnTop )
  269.                SetWindowPos( GetParent( hDlg ), HWND_TOPMOST, 0, 0, 0, 0,
  270.                              SWP_NOMOVE | SWP_NOSIZE );
  271.             else
  272.                SetWindowPos( GetParent( hDlg ), HWND_NOTOPMOST, 0, 0, 0, 0,
  273.                              SWP_NOMOVE | SWP_NOSIZE );
  274.  
  275.             EndDialog( hDlg, TRUE );
  276.             return( TRUE );
  277.          }
  278.          break;
  279.    }
  280.    return( FALSE );
  281. }
  282.